<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/16
 * Time: 12:42
 */

namespace App\Spider;

use EasySwoole\RedisPool\Redis;

class RedisLogic
{
    static function addProduce($data) //生产
    {
        return Redis::invoker('redis',function (\Swoole\Coroutine\Redis $redis)use($data){
            $produceListKey = 'easyswooleProduceList';
            if( is_string($data) ){
                $data = [
                    'url' => $data,
                ];
            }
            if( self::checkProduceMap($data['url']) ){
                return true;
            }
            self::addProduceMap($data['url']);
            $result = $redis->rPush($produceListKey,$data);
            return $result;
        },0);
    }

    static function checkProduceMap($url)
    {
        return Redis::invoker('redis',function (\Swoole\Coroutine\Redis $redis)use($url){
            $produceUrlMapKey = 'produceUrlMapKey';
            $result = $redis->hGet($produceUrlMapKey,$url);
            return (bool)$result;
        },0);
    }

    static function addProduceMap($url,$state=1)
    {
        return Redis::invoker('redis',function (\Swoole\Coroutine\Redis $redis)use($url,$state){
            $produceUrlMapKey = 'produceUrlMapKey';
            $result = $redis->hSet($produceUrlMapKey,$url,$state);
            return (bool)$result;
        },0);
    }

    static function addConsume($data) //消耗
    {
        return Redis::invoker('redis',function (\Swoole\Coroutine\Redis $redis)use($data){
            $consumeListKey = 'easyswooleConsumeList';
            if( is_string($data) ){
                $data = [
                    'url' => $data
                ];
            }
//            if( self::checkConsumeMap($data['url']) ){
//                return true;
//            }
//            self::addConsumeMap($data['url']);
            $result = $redis->rPush($consumeListKey,$data);
            return $result;
        },0);
    }

    static function checkConsumeMap($url)
    {
        return Redis::invoker('redis',function (\Swoole\Coroutine\Redis $redis)use($url){
            $consumeUrlMapKey = 'consumeUrlMapKey';
            $result = $redis->hGet($consumeUrlMapKey,$url);
            return (bool)$result;
        },0);
    }

    static function addConsumeMap($url,$state=1)
    {
        return Redis::invoker('redis',function (\Swoole\Coroutine\Redis $redis)use($url,$state){
            $consumeUrlMapkey = 'consumeUrlMapKey';
            $result = $redis->hSet($consumeUrlMapkey,$url,$state);

            return (bool)$result;
        },0);
    }

    static function clearConsumeMap()
    {
        return Redis::invoker('redis',function (\Swoole\Coroutine\Redis $redis){
            $consumeUrlMapKey = 'consumeUrlMapKey';
            $result = $redis->delete($consumeUrlMapKey);
            return (bool)$result;
        },0);
    }

    static function clearProduceMap()
    {
        return Redis::invoker('redis',function (\Swoole\Coroutine\Redis $redis){
            $produceUrlMapKey = 'produceUrlMapKey';
            $result = $redis->delete($produceUrlMapKey);
            return (bool)$result;
        },0);
    }

    static function clearConsumeList()
    {
        return Redis::invoker('redis',function (\Swoole\Coroutine\Redis $redis){
            $consumeListKey = 'easyswooleConsumeList';
            $result = $redis->delete($consumeListKey);
            return (bool)$result;
        },0);
    }

    static function clearProduceList()
    {
        return Redis::invoker('redis',function (\Swoole\Coroutine\Redis $redis){
            $produceListKey = 'easyswooleProduceList';
            $result = $redis->delete($produceListKey);
            return (bool)$result;
        },0);
    }

}